﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyVoteMVC.ExcelObject;
using MyVoteMVC.BackstageManagement.Controllers;
using System.IO;
using MyVoteMVC.Data;
using MyVoteMVC.Domain;
using MyVoteMVC.Common;
using MyVoteMVC.Common.Objects;
using MyVoteMVC.Data.Repositories;


namespace MyVoteMVC.BackstageManagement.Views
{
    public partial class UsersManagement : Form
    {
        private IUsersController controller;

        public UsersManagement()
        {
            InitializeComponent();
            controller = new UsersManagementController();
        }

        private void UserImportor_Load(object sender, EventArgs e)
        {
            Init();
        }

        private void Init()
        {
            //初始化领导和用户的列表
            IList<VT_Users> v_Users = controller.GetAdministratorList();
            InitLvAD(v_Users);
            v_Users = controller.GetLeaderUsersList();
            Initlvleader(v_Users);
            v_Users = controller.GetOrdinaryUsersList();
            InitlvUser(v_Users);  
        }

        private void InitLvAD(IList<VT_Users> v_Users)
        {
            LvAD.Items.Clear();
            foreach (VT_Users vs in v_Users)
            {
                ListViewItem lvItem = new ListViewItem();
                lvItem.Text = vs.User_RealName;
                lvItem.SubItems.Add(vs.User_Email);
                lvItem.SubItems.Add(vs.User_Name);
                lvItem.SubItems.Add((vs.User_IfAdminValue == 1) ? "是" : "否");
                lvItem.SubItems.Add((vs.User_IfLeaderValue == 1) ? "是" : "否");
                LvAD.Items.Add(lvItem);
            }
             
        }

        private void InitlvUser(IList<VT_Users> v_Users)
        {
            lvUser.Items.Clear();
            foreach (VT_Users vs in v_Users)
            {
                ListViewItem lvItem = new ListViewItem();
                lvItem.Text = vs.User_RealName;
                lvItem.SubItems.Add(vs.User_Email);
                lvItem.SubItems.Add(vs.User_Name);
                lvItem.SubItems.Add((vs.User_IfAdminValue == 1) ? "是" : "否");
                lvItem.SubItems.Add((vs.User_IfLeaderValue == 1) ? "是" : "否");
                lvUser.Items.Add(lvItem);
            }
        }

        private void Initlvleader(IList<VT_Users> v_Users)
        {
            lvleader.Items.Clear();
            foreach (VT_Users vs in v_Users)
            {
                ListViewItem lvItem = new ListViewItem();
                lvItem.Text = vs.User_RealName;
                lvItem.SubItems.Add(vs.User_Email);
                lvItem.SubItems.Add(vs.User_Name);
                lvItem.SubItems.Add((vs.User_IfAdminValue == 1) ? "是" : "否");
                lvItem.SubItems.Add((vs.User_IfLeaderValue == 1) ? "是" : "否");
                lvleader.Items.Add(lvItem);
            } 
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                if (string.IsNullOrEmpty(txtInitPassword.Text.Trim()))
                {
                    MessageBox.Show("初始密码不能为空！");
                    return;
                }
                if (txtInitPassword.Text.Trim() != txtMSInitPassword.Text.Trim())
                {
                    MessageBox.Show("初始密码与确认密码不一致！");
                    return;
                }
                int iAD = 0;
                int ileader = 0;
                if (cboAddAD.Text.Trim() == "是") iAD = 1;
                if (cboAleader.Text.Trim() == "是") ileader = 1;

                // 要先检测是否存在相同的注册名称，不能为空
                int iresult = controller.AddUser(txtAEmail.Text.Trim(), txtARealName.Text.Trim(), txtInitPassword.Text.Trim(), iAD, ileader);
                if (iresult == 1)
                {
                    MessageBox.Show("成功增加");
                    btnClear_Click(null, null);
                    Init();
                    //刷新列表
                }
                else if (iresult == 2)
                {
                    MessageBox.Show("该用户已经存在");
                }
                this.Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Default;
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtAEmail.Text = "";
            lblAddUser.Text = "";
            txtARealName.Text = "";
            cboAddAD.Text = "";
            cboAleader.Text = "";
            txtInitPassword.Text = "";
            txtMSInitPassword.Text = "";
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (lblEmail.Text.Trim() == "")
            {
                MessageBox.Show("没有选中要删除的用户！");
                return;
            }
            if (MessageBox.Show("是否要删除用户：" + lblEmail.Text.Trim(), "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                return;
            controller.DeleteUser(lblEmail.Text.Trim());
            MessageBox.Show("删除成功！");
            Init();
            clear();
            //刷新列表
            return;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (lblEmail.Text.Trim() == "")
            {
                MessageBox.Show("没有选中要删除的用户！");
                return;
            }
            int iAD = 0;
            int ileader = 0;
            if (cboAdministrator.Text.Trim() == "是") iAD = 1;
            if (cboleader.Text.Trim() == "是") ileader = 1;
            bool bl = false;
            //如果是修改密码不为空，则增加修改密码的保存
            if (txtPassWord.Text.Trim() != "")
            {
                if (txtPassWord.Text.Trim() != txtMSPassWord.Text.Trim())
                {
                    MessageBox.Show("修改密码与确认密码不一致！");
                    return;
                }
                bl = controller.EditUser(lblEmail.Text.Trim(), txtRealName.Text.Trim(), iAD, ileader, txtPassWord.Text.Trim());
            }
            else
            {
                bl = controller.EditUser(lblEmail.Text.Trim(), txtRealName.Text.Trim(), iAD, ileader);
            }

            if (bl == true)
            {
                MessageBox.Show("修改成功！");
                Init();
            }
            else
            {
                MessageBox.Show("修改失败！");
            }
            txtPassWord.Text = "";
            txtMSPassWord.Text = "";
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                MessageBox.Show("请填写查找信息");
                return;
            }
            IList<VT_Users> v_Users = controller.FindUsersList(txtName.Text.Trim());
            lvPerson.Items.Clear();
            foreach (VT_Users vs in v_Users)
            {
                ListViewItem lvItem = new ListViewItem();
                lvItem.Text = vs.User_RealName;
                lvItem.SubItems.Add(vs.User_Email);
                lvItem.SubItems.Add(vs.User_Name);
                lvItem.SubItems.Add((vs.User_IfAdminValue == 1) ? "是" : "否");
                lvItem.SubItems.Add((vs.User_IfLeaderValue == 1) ? "是" : "否");
                lvPerson.Items.Add(lvItem);
            } 
        }

        private void lvleader_Click(object sender, EventArgs e)
        {
            if (this.lvleader.SelectedItems != null)  //判断ListView控件是否有项目选中
            {
                ShowDetail(this.lvleader.SelectedItems[0]);
            }

        }

        private void lvUser_Click(object sender, EventArgs e)
        {
            if (this.lvUser.SelectedItems != null)   
            {
                ShowDetail(this.lvUser.SelectedItems[0]);
            }

        }

        private void LvAD_Click(object sender, EventArgs e)
        {
            if (this.LvAD.SelectedItems != null)   
            {
                ShowDetail(this.LvAD.SelectedItems[0]);
            }

        }

        private void lvPerson_Click(object sender, EventArgs e)
        {
            if (this.lvPerson.SelectedItems != null)
            {
                ShowDetail(this.lvPerson.SelectedItems[0]);
            }

        }

        private void ShowDetail(ListViewItem lvItem)
        {
            txtRealName.Text = lvItem.SubItems[0].Text;
            lblEmail.Text = lvItem.SubItems[1].Text;
            lblUser.Text = lvItem.SubItems[2].Text;
            cboAdministrator.Text = lvItem.SubItems[3].Text;
            cboleader.Text = lvItem.SubItems[4].Text;
        }

        private void clear()
        {
            lblUser.Text = "";
            txtRealName.Text = "";
            lblEmail.Text = ""; 
            cboAdministrator.Text = "";
            cboleader.Text = "";
            txtPassWord.Text = "";
            txtMSPassWord.Text = "";
        }

        
    }
}
